package xin.yangshuai.redis01.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisClusterCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import xin.yangshuai.redis01.service.OrderService;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class OrderServiceImpl implements OrderService {

    public static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void addOrder() {
        int keyId = ThreadLocalRandom.current().nextInt(1000) + 1;
        String serialNo = UUID.randomUUID().toString();

        String key = "order:" + keyId;
        String value = "order:" + serialNo;

        redisTemplate.opsForValue().set(key, value);

        logger.info("redis key: {}", key);
        logger.info("redis value: {}", value);

        String key1 = "order订单:" + keyId;
        String value1 = "order内容:" + serialNo;

        redisTemplate.opsForValue().set(key1, value1);

        logger.info("redis key: {}", key1);
        logger.info("redis value: {}", value1);
    }

    @Override
    public String getOrderById(Integer keyId) {

        String key = "order:" + keyId;

        String value = (String) redisTemplate.opsForValue().get(key);

        logger.info("redis key: {}", key);
        logger.info("redis value: {}", value);

        String key1 = "order订单:" + keyId;

        String value1 = (String) redisTemplate.opsForValue().get(key1);

        logger.info("redis key: {}", key1);
        logger.info("redis value: {}", value1);

        return value;
    }

    @Override
    public void clusterTest() {

        RedisClusterCommands redisClusterCommands = redisTemplate.getConnectionFactory().getClusterConnection().clusterCommands();

        logger.info("k0 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k0".getBytes()));
        logger.info("k1 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k1".getBytes()));
        logger.info("k2 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k2".getBytes()));
        logger.info("k3 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k3".getBytes()));
        logger.info("k4 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k4".getBytes()));
        logger.info("k5 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k5".getBytes()));
        logger.info("k6 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k6".getBytes()));
        logger.info("k7 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k7".getBytes()));
        logger.info("k8 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k8".getBytes()));
        logger.info("k9 keyslot: " + redisClusterCommands.clusterGetSlotForKey("k9".getBytes()));

        redisTemplate.opsForValue().set("k0", "hello");
        redisTemplate.opsForValue().set("k1", "hello");
        redisTemplate.opsForValue().set("k2", "hello");
        redisTemplate.opsForValue().set("k3", "hello");
        redisTemplate.opsForValue().set("k4", "hello");
        redisTemplate.opsForValue().set("k5", "hello");
        redisTemplate.opsForValue().set("k6", "hello");
        redisTemplate.opsForValue().set("k7", "hello");
        redisTemplate.opsForValue().set("k8", "hello");
        redisTemplate.opsForValue().set("k9", "hello");

        String k0 = (String) redisTemplate.opsForValue().get("k0");
        String k1 = (String) redisTemplate.opsForValue().get("k1");
        String k2 = (String) redisTemplate.opsForValue().get("k2");
        String k3 = (String) redisTemplate.opsForValue().get("k3");
        String k4 = (String) redisTemplate.opsForValue().get("k4");
        String k5 = (String) redisTemplate.opsForValue().get("k5");
        String k6 = (String) redisTemplate.opsForValue().get("k6");
        String k7 = (String) redisTemplate.opsForValue().get("k7");
        String k8 = (String) redisTemplate.opsForValue().get("k8");
        String k9 = (String) redisTemplate.opsForValue().get("k9");

        logger.info("redis value: {}", k0);
        logger.info("redis value: {}", k1);
        logger.info("redis value: {}", k2);
        logger.info("redis value: {}", k3);
        logger.info("redis value: {}", k4);
        logger.info("redis value: {}", k5);
        logger.info("redis value: {}", k6);
        logger.info("redis value: {}", k7);
        logger.info("redis value: {}", k8);
        logger.info("redis value: {}", k9);
    }
}
